package com.wsoft.bpm.service;

import cn.hutool.core.util.StrUtil;
import com.wsoft.bpm.component.DynamicJumpCmd;
import org.activiti.engine.ManagementService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 动态节点跳过服务
 *
 * @Author zhonglj
 * @Date 2024-10-08 11:15
 */
@Service
public class DynamicJumpService {
    @Resource
    protected ManagementService managementService;

    @Resource
    private TaskService taskService;

    /**
     * 跳转至任意节点
     *
     * @param processInstanceId
     * @param fromActivityId
     * @param toActivityId
     * @param comment
     * @author zhonglj
     * @date 2024-10-08 13:24
     **/
    public void jumpTask(String processInstanceId, String fromActivityId, String toActivityId, String comment) {
        Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
        if (StrUtil.isNotBlank(comment)) {
            taskService.addComment(task.getId(), processInstanceId, comment);
        }
        managementService.executeCommand(new DynamicJumpCmd(processInstanceId, fromActivityId, toActivityId));
    }
}
